#include <stdio.h>
#include <stdlib.h>

struct ListNode
{
    int data;
    struct ListNode* prev;
    struct ListNode* next;
};
struct ListNode*   ListInit()
{
    struct ListNode* phead=(struct ListNode*)malloc(sizeof(struct ListNode));
    phead->next=phead;
    phead->prev=phead;
    return phead;
}
void ListPushBack(struct ListNode* phead,int x)
{
    struct ListNode* newnode;
    struct ListNode* tail=phead->prev;
    newnode=(struct ListNode*)malloc(sizeof(struct ListNode));
    newnode->data=x;
    tail->next=newnode;
    newnode->prev=tail;
    phead->prev=newnode;
    newnode->next=phead;
}
void ListPop(struct ListNode* cur)
{
    struct ListNode* ne=cur->next;
    struct ListNode* pre=cur->prev;
    pre->next=ne;
    ne->prev=pre;
    free(cur);
    cur=NULL;
}
int main()
{
    int n,m;
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        struct ListNode* plist=NULL;
        plist=ListInit();
        for(int i=0;i<n;i++)
        {
            ListPushBack(plist,i);
        }
        int j=1;
        struct ListNode* cur=plist->next;
        while(plist->next->next!=plist)
        {
            if(j!=m)
            {
                if(cur->next==plist)
                {
                    cur=cur->next->next;
                }
                else
                {
                    cur=cur->next;
                }
                j++;
                //printf("%d %d\n",j,cur->data);
            }
            if(j==m)
            {
                ListPop(cur);
                j=0;
            }
        }
        printf("%d\n",plist->next->data);
    }
    return 0;
}
